<?php
namespace app\api\controller\m2979\auto;

use think\Db;
use service\ApiReturn;
use app\api\controller\Base;


class Freeyouhui extends Base {
                    
/**
 *  操作优惠
 *@param  String[字符串]  biaoti  标题
 *@param  String[字符串]  neirong  内容
 *@create_time  2023-11-09 17:32:40
 */ 
public function set_youhui($data = [], $user = [])
{
    $return_data = [];
 
     		//入参判断优惠
		if (!($data['biaoti'] == ''  && $data['neirong'] == '')) {
			throw new \Exception('错误',-1);
		}
		//查询优惠
		$order = 'freeyouhui.create_time desc';
		$where = [];
		$where[] = ['freeyouhui.is_del', '=', 0]; 
 		if (isset($data['biaoti'])) {
		$where[] = ['freeyouhui.biaoti', '=', $data['biaoti']]; 
 		}
		$query_1 = Db::table('lb_free_youhui')
		->alias('freeyouhui')
		->where($where)
		->order($order)
		->find();
		if (!($query_1)) {
		//添加优惠
		$param=[];
		$param['biaoti']= $data['biaoti'];
		$param['neirong']= $data['neirong'];
		$param['update_time'] = time();
		$param['create_time'] = time();
		$param['is_del'] = 0;
		$param['member_id'] = (isset($user['member_id']))?$user['member_id']:0;

		$query_2 = $param;
		$table_name = 'lb_free_youhui';
		$primary_key = 'aid';
		$res = Db::table($table_name)->insertGetId($param);
		if ($res) {
			$query_2['aid'] = $res;
		}
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}
		 else{
		//更新优惠
		$where = [];
		$where[] = ['is_del', '=', 0]; 
 		if (isset($data['biaoti'])) {
		$where[] = ['biaoti', '=', $data['biaoti']]; 
 		}
		$param=[];
		$param['biaoti']= $data[''];
		$param['neirong']= $data[''];
		$param['update_time'] = time();

		$query_3 = $param;
		$table_name = 'lb_free_youhui';
		$primary_key = 'aid';
		$res = Db::table($table_name)->where($where)->update($param);
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}

        
     $return_field_data = $query_2['aid'] ?? '';
		$return_data['aid'] = $return_field_data;
$return_field_data = $query_3['aid'] ?? '';
		$return_data['aid'] = $return_field_data;

     
     		$aid = $query_2['aid'];
		\app\spread\server\Message::sendMsg($aid, 'lb_free_youhui','2979');


    return ApiReturn::r(1, $return_data, '123');
}
                
/**
 *  操作优惠
 *@param  String[字符串]  aid  当前操作的行数据ID
 *@param  String[字符串]  biaoti  标题
 *@param  String[字符串]  neirong  内容
 *@param  String[字符串]  member_id  企业ID
 *@param  String[字符串]  member_uid  当前登录UID
 *@param  String[字符串]  alias  当前操作的数据表别名
 *@param  String[字符串]  aid  当前操作的行数据ID
 *@param  String[字符串]  old_data  当前操作数据的旧数据
 *@create_time  2023-11-13 10:14:11
 */ 
public function set_youhui111($data = [], $user = [])
{
    $return_data = [];
 
     		//入参判断优惠
		if (!($data['biaoti'] != ''  && $data['neirong'] != '')) {
			throw new \Exception('参数不能为空',-1);
		}
		//查询优惠
		$order = 'freeyouhui.create_time desc';
		$where = [];
		$where[] = ['freeyouhui.is_del', '=', 0]; 
 		if (isset($data['biaoti'])) {
		$where[] = ['freeyouhui.biaoti', '=', $data['biaoti']]; 
 		}
		$query_1 = Db::table('lb_free_youhui')
		->alias('freeyouhui')
		->where($where)
		->order($order)
		->find();
		if (!($query_1)) {
		//添加优惠
		$param=[];
		$param['biaoti']= $data['biaoti'];
		$param['neirong']= $data['neirong'];
		$param['update_time'] = time();
		$param['create_time'] = time();
		$param['is_del'] = 0;
		$param['member_id'] = (isset($user['member_id']))?$user['member_id']:0;

		$query_2 = $param;
		$table_name = 'lb_free_youhui';
		$primary_key = 'aid';
		$res = Db::table($table_name)->insertGetId($param);
		if ($res) {
			$query_2['aid'] = $res;
		}
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}
		 else{
		//更新优惠
		$where = [];
		$where[] = ['is_del', '=', 0]; 
 		$where[] = ['aid', '=', $query_1['aid']  ]; 
 		$param=[];
		$param['neirong']= $data['neirong'];
		$param['update_time'] = time();

		$query_3 = $param;
		$table_name = 'lb_free_youhui';
		$primary_key = 'aid';
		$res = Db::table($table_name)->where($where)->update($param);
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}

        
     
     
     		$aid = $query_2['aid'];
		\app\spread\server\Message::sendMsg($aid, 'lb_free_youhui','2979');


    return ApiReturn::r(1, $return_data, '成功');
}

}